<?php

/*
  // special code associated with Action form
 */

function Action(&$ParamRec, &$Data, &$q, &$UE_Opts) {
    //
    //print "<br>Action.php: Work=";
    //var_dump($_SESSION['Work']);
    //
    $Text = $ParamRec[0];
    $Type = $ParamRec[1];
    $FieldVals = $ParamRec[2];
    $VText = $ParamRec[3];
    $Default = $ParamRec[4];
    parse_str($ParamRec[5], $Options);
    $VOpts = $ParamRec[6];
    $ShortName = $ParamRec[7];
    if ($ShortName === null || $ShortName === "") {
        $ShortName = $Text;
    }
    $FieldName = $ParamRec[9];
    if ($FieldName) {
        $FieldVal = $Data[$FieldName];
    } else {
        $FieldVal = "";
    }
    if ($FieldVal == "") {
        $FieldVal = $Data[$q];
    }
    //
    $Exit = $UE_Opts['Exit'];
    $Trace = $UE_Opts['Trace'];
    //
    if ($Trace) {
        print "<br>Action.php - Exit=" . $Exit . ", q=" . $q . ", FieldName=" . $FieldName . ", FieldVal=" . $FieldVal;
    }
    switch ($Exit) {
        Case "A":
            //at very start
            break;
        Case "E":
            //before field entry/import
            switch ($FieldName) {
                default:
                    //$FieldVal = "";
                    break;
            }
            break;
        Case "F":
            if ($Trace) {
                print "<br>field name=" . $FieldName . ", val=" . $FieldVal;
            }
            //after field entry/import
            switch ($FieldName) {
                case "CreateDate":
                    $FieldVal = "NOW()";
                    break;
                case "Division":
                    //print "case Division";
                    $FieldVal = "T";
                    break;
                case "ClientID":
                    if (empty($Data)) {
                        $FieldVal = $_SESSION['Work']['ClientID'];
                    }
                    //print "<br>Action.php: ClientID = " . $FieldVal;
                    break;
                case "ClaimID":
                    if (empty($Data)) {
                        $FieldVal = $_SESSION['Work']['ClaimID'];
                    }
                    //print "<br>Action.php: ClaimID = " . $FieldVal;
                    break;
                case "Status":
                    $_SESSION['Work']['Status'] = $FieldVal;
                    //print "<br>Action.php: Status = " . $FieldVal;
                    break;
                default:
                    $FieldVal = "";
                    break;
            }
            break;
        Case "Q":
            //before updates
            $Status = $Data[$q];
            $SQL4 = "SELECT Description, NextActionDays FROM status WHERE ID = '" . $Status . "'";
            //print "<br>SQL4=" . $SQL4 . "<br>";
            $result4 = mysql_query($SQL4);
            $num_rows4 = mysql_num_rows($result4);
            $Data4 = array();
            if ($result4) {
                if ($num_rows4 > 0) {
                    $Data4 = mysql_fetch_assoc($result4);
                    $SQL5 = "UPDATE `claims` SET `Status`='" . $Data4['Description'];
                    date_default_timezone_set('Europe/London');
                    $Today = date("Y-m-d");
                    $nad = $Data4['NextActionDays'];
                    $SQL5 = $SQL5 . "', `StatusDate`= NOW(),";
                    $SQL5 = $SQL5 . " `NextActDate`= DATE( NOW() + INTERVAL " . $nad . " DAY )";
                    $SQL5 = $SQL5 . " WHERE `ID`='" . $Data[2] . "'";
                    if ($Trace) {
                        print "<BR>Action.php - SQL5=" . $SQL5;
                        print "<BR>Action.php - Data=";
                        var_dump($Data);
                    }
                    $result5 = mysql_query($SQL5);
                    //print "result5 = " . $result5 . ".";
                }
            }
            break;
        default:
            //do nothing!
            break;
    }
//
    if ($Trace) {
        print "<br>Action.php - Work=";
        var_dump($_SESSION['Work']);
        print ", Data=";
        var_dump($Data);
    }
    return $FieldVal;
}

?>
